<?php
	include "layout.php";
	
	head("Edit Deposit");
?>
<h1>Edit a Deposit</h1>
<?php 
	if (!isset($_GET["id"])) {
		header("Location: planets.php");
	}
	if (isset($_POST["submit"])) {
		edit_deposit();
	}
	$id = $_GET["id"];
	$depositq = mysql_query("SELECT * FROM deposits where id={$id}");
	$deposit = mysql_fetch_array($depositq);
	$size = $deposit["value"];
	$rmtype = $deposit["rmtype"];
	$planet = $deposit["planet"];
	$terrain = $deposit["terrain"];
	$x = $deposit["x"];
	$y = $deposit["y"];
	$rmtypeq = mysql_query("SELECT * FROM rmtypes ORDER BY id");
	$planetsq = mysql_query("SELECT * FROM planets ORDER BY id");
	$planetq = mysql_query("SELECT * FROM planets where id = {$planet}");
	$planetr = mysql_fetch_array($planetq);
	$planetsize = $planetr["size"];
	$terrainq = mysql_query("SELECT * FROM terrain ORDER BY id");
	echo <<<Form
<form name="editdeposit" action="editdeposit.php?id={$id}" method="post">
	<label for="value">Deposit Size:</label>
	<input type="text" name="value" placeholder="Deposit Size" value={$size} /><br />
	<label for="rmtype">RM Type:</label>
	<select name="rmtype">
		<option value=0>--</option>
Form;
	while ($rmtypes = mysql_fetch_array($rmtypeq)) {
		if ($rmtypes["id"] == $rmtype){
			echo "<option value={$rmtypes["id"]} selected>{$rmtypes["name"]}</option>";
		}
		else {
			echo "<option value={$rmtypes["id"]}>{$rmtypes["name"]}</option>";
		}
	}
	echo <<<Form
	</select><br />
	<label for="planet">Planet:</label>
	<select name="planet" onchange="showSize(this.value)">
		<option value="">--</option>
Form;
	while ($planets = mysql_fetch_array($planetsq)) {
		if ($planets["id"] == $planet){
			echo "<option value={$planets["id"]} selected>{$planets["name"]}</option>";
		}
		else {
			echo "<option value={$planets["id"]}>{$planets["name"]}</option>";
		}
	}
	
	echo <<<Form
	</select><br />
	<label for="terrain">Terrain:</label>
	<select name="terrain">
		<option value="">--</option>	
Form;
	while ($terrains = mysql_fetch_array($terrainq)) {
		if ($terrains["id"] == $terrain){
			echo "<option value={$terrains["id"]} selected>{$terrains["name"]}</option>";
		}
		else {
			echo "<option value={$terrains["id"]}>{$terrains["name"]}</option>";
		}
	}
	
	echo <<<Form
	</select><br />
	<label for="x">X-Coordinate:</label>
	<select name="x" id="x">
		<option value="">--</option>
Form;
	for ($i = 0; $i < $planetsize; $i++) {
		if ($i == $x) {
			echo "<option value={$i} selected>{$i}</option>";
		}
		else {
			echo "<option value={$i}>{$i}</option>";
		}
	}
	echo <<<Form
	</select><br />
	<label for="y">Y-Coordinate:</label>
	<select name="y" id="y">
		<option value="">--</option>
Form;
	for ($i = 0; $i < $planetsize; $i++) {
		if ($i == $y) {
			echo "<option value={$i} selected>{$i}</option>";
		}
		else {
			echo "<option value={$i}>{$i}</option>";
		}
	}
	echo <<<Form
	</select><br />
	<input type="submit" name="submit" value="Edit" /> 
</form>
Form;
?>
<?php 
	foot();
?>